package multi_ponit;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

public class FileDownloadImpl implements Runnable{
	
	private String fromName;
	private String toName;
	private long begin;
	private long end;
	
	

	public FileDownloadImpl(String fromName, String toName, long begin, long end) {
		super();
		this.fromName = fromName;
		this.toName = toName;
		this.begin = begin;
		this.end = end;
	}



	public void run() {
		try {
			InputStream in = new FileInputStream(fromName);
			RandomAccessFile af = new RandomAccessFile(toName, "rw");
			
			byte[] buf = new byte[1024];
			in.skip(begin);
			af.seek(begin);
			int len = 0;
			long count =begin;
			System.out.println("begin:"+begin+"is downloading");
			while((len=in.read(buf))!=-1&&count<end){
				af.write(buf,0,len);
				count+=len;
				if (count%1024==0) {
					System.out.println("begin:"+begin+"is downloading+++");
				}
			}
			System.out.println("begin:"+begin+"is finish");
			in.close();
			af.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
